<?php

namespace Kunfeng\Dify\Model\Datasets;

/**
 * 分段列表响应模型
 */
class SegmentListResponse
{
    private $data;
    private $total;
    private $page;
    private $limit;
    private $hasMore;

    public function __construct(array $data = [])
    {
        $this->data = array_map(function($item) {
            return Segment::fromArray($item);
        }, $data['data'] ?? []);
        $this->total = $data['total'] ?? 0;
        $this->page = $data['page'] ?? 1;
        $this->limit = $data['limit'] ?? 20;
        $this->hasMore = $data['has_more'] ?? false;
    }

    /**
     * 从数组创建
     */
    public static function fromArray(array $data): self
    {
        return new self($data);
    }

    // Getters
    public function getData(): array
    {
        return $this->data;
    }

    public function getTotal(): int
    {
        return $this->total;
    }

    public function getPage(): int
    {
        return $this->page;
    }

    public function getLimit(): int
    {
        return $this->limit;
    }

    public function hasMore(): bool
    {
        return $this->hasMore;
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'data' => array_map(function($segment) {
                return $segment->toArray();
            }, $this->data),
            'total' => $this->total,
            'page' => $this->page,
            'limit' => $this->limit,
            'has_more' => $this->hasMore,
        ];
    }
}
